package com.warm.base.controller;

import com.warm.base.jdk.anno.User;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author warmchuang
 * @date 2023/7/13 15:34
 * @desc
 */
@RestController
public class AnnoValidatorController {

    @PostMapping("/anno")
    public String annoValid(@Valid @RequestBody User user, BindingResult res){
        if(res.hasErrors()){
            res.getAllErrors().forEach(e -> {
                System.out.println(e.getObjectName() + ":" + e.getDefaultMessage());
            });
        }
        return "validate";
    }

    @PostMapping("/anno0")
    public String annoValid(@Valid @RequestBody User user){
        return "validate";
    }

    @PostMapping("/anno1")
    public String annoValid1(@Validated @RequestBody User user){
        return "validate";
    }

    @PostMapping("/anno2")
    public String annoValid2(@RequestBody User user){
        return "validate";
    }
}
